package com.leyou.auth.config;

import com.leyou.auth.task.PrivilegeTokenHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.auth.config
 * @date 2019/7/10 20:00
 * @description:
 **/
@Configuration
public class FeignConfig {


    @Bean
    public RequestInterceptor requestInterceptor(JwtProperties prop, PrivilegeTokenHolder tokenHolder){
        return new PrivilegeRequestInterceptor(prop,tokenHolder);
    }

    class PrivilegeRequestInterceptor implements RequestInterceptor {

        private JwtProperties prop;

        private PrivilegeTokenHolder tokenHolder;

        public PrivilegeRequestInterceptor(JwtProperties prop, PrivilegeTokenHolder tokenHolder) {
            this.prop = prop;
            this.tokenHolder = tokenHolder;
        }

        @Override
        public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header(prop.getApp().getHeaderName(),tokenHolder.getToken());
        }
    }



}
